package com.wiz.weixin.controllor;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wiz.weixin.common.ResponseCode;
import com.wiz.weixin.manager.TencentManager;
import com.wiz.weixin.service.FuellingDataService;
import com.wiz.weixin.service.MemberService;
import com.wiz.weixin.service.StationService;

/**
 * 加油记录
 * @author xxy
 */
@Controller
@RequestMapping("/fuellingData")
public class FuellingDataControllor implements ResponseCode{
	
	@Resource
	private TencentManager tencentManager;
	@Resource
	private MemberService memberService;
	@Resource
	private FuellingDataService fuellingDataService;
	@Resource
	private StationService stationService;
	
	/**
	 * 跳转加油页面
	 * @throws Exception 
	 */
	@RequestMapping(value="/toRecord")
	public String records(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model) throws Exception{
		String openId = (String) session.getAttribute("openid");
		if (StringUtils.isBlank(openId)) {
			String code = request.getParameter("code");
			openId = tencentManager.getOpenId(code);
			session.setAttribute("openid", openId);
		}
		if(StringUtils.isBlank(openId)){
			model.addAttribute("msg", "未找到微信关联用户");
			return "/error";
		}
//			String openid = "ovNRGs45WudglvjfjUth3W57tJOU";
		List<String> hqIds = stationService.getHQIds(openId);
		if(hqIds == null || hqIds.size()<= 0){
			model.addAttribute("msg", "对不起，您还未注册，请扫描加油站的二维码进行注册");
			return "/error";
		}
		return "/fuelling/record";
		
	}
	
	/**
	 * 查询加油记录
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	public String getFuellingDataListByOpenId(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "pageNo", required = false,defaultValue="1") int pageNo,
			@RequestParam(value = "pageSize", required = false,defaultValue="10") int pageSize,
			HttpSession session, Model model){
		String openid = (String) session.getAttribute("openid");
//			String openid = "ovNRGs45WudglvjfjUth3W57tJOU";
		return fuellingDataService.getFuellingDataListByOpenId(openid,pageNo,pageSize);
	}
	
	@RequestMapping(value="/cnt")
	@ResponseBody
	public String getCntFuellingDataByOpenId(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "pageNo", required = false,defaultValue="1") int pageNo,
			@RequestParam(value = "pageSize", required = false,defaultValue="10") int pageSize,
			HttpSession session, Model model){
		String openid = (String) session.getAttribute("openid");
//			String openid = "ovNRGs45WudglvjfjUth3W57tJOU";
		return fuellingDataService.getCntFuellingDataByOpenId(openid,pageNo,pageSize);
	}

}
